package com.hlg.xboot.model.modules;

import com.alibaba.fastjson.annotation.JSONField;
import com.hlg.xboot.enums.Channel;
import com.hlg.xboot.enums.Gender;
import com.hlg.xboot.model.BaseModel;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hlg.xboot.enums.Status;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * <p>Description:用户</p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 中视数讯</p>
 * <p>Site: www.digi-zones.com</p>
 *
 * @author yangxin
 * @version 1.0
 * @date 2016/10/24
 */
@Entity
@DynamicUpdate
@DynamicInsert
public class User extends BaseModel {

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 用户名
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 性别
     * 0：男
     * 1：女
     * @see Gender
     */
    @Column(length = 1)
    private Integer sex;

    /**
     * 生日
     */
    @JSONField (format="yyyy-MM-dd")
    @Temporal(TemporalType.DATE)
    private Date birthday;

    /**
     * 手机
     */
    @Column(length = 11, unique = true)
    private String phone;

    /**
     * 头像
     */
    @Column(name = "avatar", nullable = false, length = 300)
    private String avatar;

    /**
     * 所在省份
     */
    private String province;

    /**
     * 所在城市
     */
    private String city;

    /**
     * 国籍
     */
    private String country;

    /**
     * 微信openid
     */
    @Column(length = 30, unique = true)
    private String openid;

    /**
     * 最后登录的时间
     */
    @JsonIgnore
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_login_time")
    private Date lastLoginTime;

    /**
     * 最后登录的IP
     */
    @JsonIgnore
    @Column(name = "last_login_ip")
    private String lastLoginIP;

    /**
     * 用户渠道来源
     * @see Channel
     */
    private Integer channel;

    /**
     * 状态
     * @see Status
     */
    private Integer states;

    public User() {
    }

    public Integer getStates() {
        return states;
    }

    public void setStates(Integer states) {
        this.states = states;
    }

    public Integer getChannel() {
        return channel;
    }

    public void setChannel(Integer channel) {
        this.channel = channel;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public User(Long id) {
        this.id = id;
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getLastLoginIP() {
        return lastLoginIP;
    }

    public void setLastLoginIP(String lastLoginIP) {
        this.lastLoginIP = lastLoginIP;
        this.setLastLoginTime(new Date());
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        User userModel = (User) o;

        return new EqualsBuilder()
                .append(id, userModel.id)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(id)
                .toHashCode();
    }
}
